/*
 * Copyright (c) 2011, Brookhaven National Laboratory
 * All rights reserved.
 * The software in this package is published under the terms
 * of the BSD license a copy of which has been included with
 * this distribution in the license.txt file.
 */

#include <iostream>

#include "oxtypes/impl/DynamicTypeManagerImpl.h"

#include <tdds/xtypes/DynamicType.hpp>
#include "dds/type/dynamic/detail/DynamicType.h"

#include "oxtypes/impl/PrimitiveTypesImpl.h"

using dds::type::dynamic::DynamicType;

namespace oxtypes { namespace impl {

std::map<std::string, DynamicType> types_;

// DynamicTypeManagerImpl

DynamicTypeManagerImpl*  DynamicTypeManagerImpl::singleton = 0;

DynamicTypeManagerImpl* DynamicTypeManagerImpl::getInstance() {
  if(singleton == 0){
    singleton = new DynamicTypeManagerImpl();
  }
  return singleton;
}

DynamicTypeManagerImpl::DynamicTypeManagerImpl() {
}

// Nil Value

DynamicTypeImpl nilType_;

// Primitive Types

BooleanTypeImpl boolType;
Int32TypeImpl int32Type;
Int64TypeImpl int64Type;

DynamicType
DynamicTypeManagerImpl::getPrimitiveType(dds::type::TypeKind kind) const {
  switch(kind){
  case dds::type::BOOLEAN_TYPE: return boolType;
  case dds::type::INT_32_TYPE:  return int32Type;
  case dds::type::INT_64_TYPE:  return int64Type;
  deafult: return nilType_;
  }
}

DynamicType 
DynamicTypeManagerImpl::loadType(const DynamicType& type){
  switch(type.kind()){
  case dds::type::UNION_TYPE:
  case dds::type::STRUCTURE_TYPE:
  case dds::type::ANNOTATION_TYPE: 
    types_.insert(std::pair<std::string, DynamicType>(type.name(), type)); 
    return type;
  default: return nilType_;
  }
}
  
DynamicType 
DynamicTypeManagerImpl::getType(const std::string& name){
  std::map<std::string, DynamicType>::iterator it = types_.find(name);
  if(it != types_.end()) return it->second;
  return nilType_;
}

}}
